package com.junko.api.interceptor;

import com.junko.core.exception.BusinessException;
import com.junko.core.tool.ToolWeb;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * api访问拦截器
 */
public class ApiInterceptor extends HandlerInterceptorAdapter {
    private static final Logger logger = LogManager.getLogger(ApiInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws BusinessException {
        String actionKey = ToolWeb.getActionKey(request);
        String access_token = request.getHeader("access_token");
        if(access_token==null){
            throw new BusinessException("access_token缺失，请求actionKey："+actionKey);
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
    }
}